#include <stdlib.h>
#include "bytecode.h"
#include "bytecode_buffer.h"

void bytecode_ref(bytecode_t *self)
{
	if (self)
		self->ref++;
}

void bytecode_unref(bytecode_t *self)
{
	if (self && --(self->ref) == 0)
		free(self);
}

void bytecode_clear(bytecode_t *self)
{
	if (self->opcode & OP_OBJECT)
		Red_UNREF(self->at_object);
}

void bytecode_release(bytecode_t **self)
{
	if (self)
	{
		bytecode_unref(*self);
		*self = NULL;
	}
}

bytecode_t *bytecode_create(opcode_t opcode, int lineno, int position)
{
	bytecode_t *result = malloc(sizeof(bytecode_t));

	result->ref = 1;
	result->opcode = opcode;
	result->lineno = lineno;
	result->position = position;
	return result;
}
